package com.wyq.appsearch;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.CompoundButton;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Update by wyq on 2017/4/24.
 */
public class MainActivity extends AppCompatActivity {

    @BindView(R.id.et_text)
    AppCompatEditText textEt;
    @BindView(R.id.btn_search)
    AppCompatButton searchBtn;
    @BindView(R.id.sc)
    SwitchCompat sc;
    @BindView(R.id.rv_app)
    RecyclerView appRv;

    private List<App> allAppList = new ArrayList<>();
    private List<App> userAppList = new ArrayList<>();
    private List<App> appList = new ArrayList<>();
    private AppAdapter appAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initViews();
        initDatas();
    }

    private void initViews() {
        appAdapter = new AppAdapter(this, appList);
        appRv.setAdapter(appAdapter);
        appRv.setLayoutManager(new LinearLayoutManager(this));

        searchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppUtil.search(MainActivity.this, textEt.getText().toString());
            }
        });
        sc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                appList.clear();
                if (isChecked) {
                    appList.addAll(allAppList);
                } else {
                    appList.addAll(userAppList);
                }
                Collections.sort(appList, new AppComparator());
                appAdapter.notifyDataSetChanged();
            }
        });
    }

    private void initDatas() {
        PackageManager pm = this.getPackageManager();
        List<PackageInfo> packages = pm.getInstalledPackages(0);
        for (PackageInfo info : packages) {
            App app = new App();
            app.setFirstInstallTime(info.firstInstallTime);
            app.setIcon(info.applicationInfo.loadIcon(pm));
            app.setName(info.applicationInfo.loadLabel(pm).toString());
            app.setVersionName(info.versionName);
            app.setVersionCode(info.versionCode);
            app.setPkg(info.packageName);
            app.setIsUser((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0);
            if (app.getIsUser()) {
                userAppList.add(app);
            }
            allAppList.add(app);
        }

        appList.clear();
        if (sc.isChecked()) {
            appList.addAll(allAppList);
        } else {
            appList.addAll(userAppList);
        }
        Collections.sort(appList, new AppComparator());
        appAdapter.notifyDataSetChanged();
    }

    class AppComparator implements Comparator<App> {
        @Override
        public int compare(App o1, App o2) {
            if (o1.getFirstInstallTime() < o2.getFirstInstallTime()) {
                return 1;
            } else if (o1.getFirstInstallTime() > o2.getFirstInstallTime()) {
                return -1;
            } else {
                return 0;
            }
        }
    }

}